<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>testwebsocket</title>
</head>
<body>
<script type="text/javascript">
    function websockets() {
        if("WebSocket" in window){
            alert("你的浏览器支持websocket")
            var ws=new WebSocket("wss://echo.websocket.org")
            ws.onopen = function () {
                console.log("连接已经建立，信息就绪")
                ws.send("江杰很帅")
            }

            ws.onmessage = function (evt) {
                var res = evt.data;
                console.log("我们收到的信息是:"+res)
            }

            ws.onerror = function (evt,e) {
                console.log(e)
            }

            ws.onclose = function () {
                console.log("连接已经关闭")
            }
        }else {
            alert("抱歉，您的浏览器不支持websocket")
        }
    }
</script>
<div id="test">
    <a href="javascript:websockets()">立即运行</a>
</div>
</body>
</html>